﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HelloWPF.DataBinding.DBAccess;
using HelloWPF.DataBinding.Model;
using HelloWPF.DataBinding.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HelloWPF.DataBinding
{
    public class DataGridViewModel : ViewModelBase
    {
        LocalDBService dbService;//服务
        public ObservableCollection<Teacher> _teachers;
        public ObservableCollection<Teacher> Teachers
        {
            get { return _teachers; }
            set
            {
                _teachers = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Path右键菜单
        /// </summary>
        private System.Windows.Controls.ContextMenu _pathContextMenu;
        public System.Windows.Controls.ContextMenu PathContextMenu
        {
            get { return _pathContextMenu; }
            set
            {
                _pathContextMenu = value;
                RaisePropertyChanged();
            }
        }

        public Teacher _selectedTeacher;
        public Teacher SelectedTeacher
        {
            get { return _selectedTeacher; }
            set
            {
                _selectedTeacher = value;
                RaisePropertyChanged();
            }
        }

        private string _search = string.Empty;
        public string Search
        {
            get { return _search; }
            set { _search = value; }
        }

        #region 命令属性
        public RelayCommand QueryCommand { get; set; }

        public RelayCommand ResetCommand { get; set; }

        public RelayCommand<string> EditCommand { get; set; }

        public RelayCommand<string> DeleCommand { get; set; }

        public RelayCommand<string> DeleSelectedCommand { get; set; }

        public RelayCommand<string> PathMouseRightButtonUpCommand { get; set; }
        #endregion

        public DataGridViewModel()
        {
            dbService = new LocalDBService();
            QueryCommand = new RelayCommand(Query);
            ResetCommand = new RelayCommand(() =>
            {
                Search = string.Empty;
                this.Query();
            });
            EditCommand = new RelayCommand<string>((t) =>
            {
                this.Edit(t);
            });
            DeleCommand = new RelayCommand<string>((t) =>
            {
                this.Dele(t);
            });
            DeleSelectedCommand = new RelayCommand<string>((t) =>
            {
                this.DeleSelected();
            });
            PathMouseRightButtonUpCommand = new RelayCommand<string>((t) =>
            {
                System.Windows.Controls.ContextMenu contextMenu = new System.Windows.Controls.ContextMenu();
                System.Windows.Controls.MenuItem menuItem = new System.Windows.Controls.MenuItem();
                menuItem.Header = "Calibration the path";
                contextMenu.Items.Add(menuItem);
                PathContextMenu = contextMenu;
            });
        }

        /// <summary>
        /// 查询老师
        /// </summary>
        public void Query()
        {
            Teachers = new ObservableCollection<Teacher>();
            List<Teacher> teacherList = dbService.GetTeacherByName(Search);
            teacherList.ForEach(t =>
            {
                Teachers.Add(t);
            });
        }

        /// <summary>
        /// 编辑老师
        /// </summary>
        /// <param name="teacher_id"></param>
        public void Edit(string teacher_id)
        {
            Teacher teacher = dbService.GetTeacherById(teacher_id);
            if (teacher != null)
            {
                //打开界面
                TeacherView teacherView = new TeacherView(teacher);
                var result = teacherView.ShowDialog();
                if (result.Value)
                {
                    Teacher oldTeacher = Teachers.FirstOrDefault(t => t.Teacher_Id == teacher_id);
                    if (oldTeacher != null)
                    {
                        oldTeacher.Teacher_Name = teacher.Teacher_Name;
                    }
                }
            }
        }

        /// <summary>
        /// 删除老师
        /// </summary>
        /// <param name="teacher_id"></param>
        public void Dele(string teacher_id)
        {
            Teacher teacher = dbService.GetTeacherById(teacher_id);
            if (teacher != null)
            {
                var result = MessageBox.Show($"确认删除当前老师吗？{teacher.Teacher_Name}", "操作提示", MessageBoxButton.OK, MessageBoxImage.Question);
                if (result == MessageBoxResult.OK)
                {
                    dbService.DeleteTeacher(teacher_id);
                    this.Query();
                }
            }
        }

        /// <summary>
        /// 删除选中的老师
        /// </summary>
        /// <param name="teacher_id"></param>
        public void DeleSelected()
        {
            if (Teachers != null && Teachers.Count > 0)
            {
                Teachers.Remove(SelectedTeacher);
            }
        }
    }
}
